package com.fingard.dsp.bank.directbank.bozz02;

import java.util.Calendar;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 郑州银行历史明细
 * 
 * @author Cruise
 * @date 2018/11/19
 */
public class BOZZ02Transaction extends BOZZ02Base implements ITransaction {
    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        retAts.writeStart();
        
        String tmpRespCode = "";
        String tmpRespInfo = "";

        try {
        	Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());

            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
                    tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                } else {
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }

                BOZZ02Header tmpBankHeader = new BOZZ02Header();
                tmpBankHeader.trsCode = "B2EActTrsQry";
                tmpBankHeader.erpJnlNo = p_req7516.reqHeader.transSeqID;
                tmpBankHeader.erpTrsTimestamp = Format.DateTimeFormat.format(new Date());
                
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHead(xmlWriter, tmpBankHeader);
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("AcNo", p_req7516.actNum);
                xmlWriter.writeElementString("BeginDate", Format.Date8Format.format(tmpCalendarFrom.getTime()));
                xmlWriter.writeElementString("EndDate", Format.Date8Format.format(tmpClndrStepTo.getTime()));
                xmlWriter.writeElementString("PageCount", "50");
                xmlWriter.writeElementString("PageNo", "1");

                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), "B2EActTrsQry", false,"0");

                if (tmpStrRet[0].length() != 0) {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                    break;
                }
                int page = analysis7516Loop(tmpStrRet, retAts);
                for (int i = 2; i <= page; i++) {
                    xmlWriter = new XmlTextWriter();
                    tmpBankHeader.erpJnlNo = getQryTransID();
                    writeCommonHead(xmlWriter, tmpBankHeader);
                    xmlWriter.writeStartElement("Body");
                    xmlWriter.writeElementString("AcNo", p_req7516.actNum);
                    xmlWriter.writeElementString("BeginDate", p_req7516.dateFrom);
                    xmlWriter.writeElementString("EndDate", p_req7516.dateTo);
                    xmlWriter.writeElementString("PageCount", "50");
                    xmlWriter.writeElementString("PageNo", i + "");
                    xmlWriter.writeEndDocument();
                    tmpStrRet = sendToBank(xmlWriter.toXmlString(), "B2EActTrsQry", false,"0");
                    page = analysis7516Loop(tmpStrRet, retAts);
                }
                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception ex) {
        	ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        
        if ("-1".equals(tmpRespCode)) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);
        tmpReq7516.actNum = p_req7515.actNum;
        String tmpStrToday = Format.Date8Format.format(new Date());
        tmpReq7516.dateFrom = tmpStrToday;
        tmpReq7516.dateTo = tmpStrToday;
        return d_7516_HisTrans(tmpReq7516);
    }

    public int analysis7516Loop(String[] tmpStrRet, RetAtsTrans retAts) throws Exception {
        int page = -1;
        RetAtsTransDetail detail = new RetAtsTransDetail();
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String totalCount = tmpBkReader.getFirstText("/Message/Body/TotleCount");
                if (StringHelper.isNullOrEmpty(totalCount)) {
                    return 0;
                }

                int tmpPage = Integer.parseInt(totalCount) / 50;
                if (Integer.parseInt(totalCount) % 50 > 0) {
                    tmpPage += 1;
                }
                page =  tmpPage;
                String bankRetCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                if ("000000".equals(bankRetCode)) {
                    String mapPath = "/Message/Body/List/Map";
                    while (tmpBkReader.read(mapPath)) {
                        detail.originator = tmpBkReader.readedXElement.getSingleText("JnlNo");
                        detail.tranDate = tmpBkReader.readedXElement.getSingleText("TransDate");
                        detail.valueDate = tmpBkReader.readedXElement.getSingleText("TransDate");
                        detail.tranTime = detail.tranDate + " " + tmpBkReader.readedXElement.getSingleText("TransTime");
                        detail.recAccBank = tmpBkReader.readedXElement.getSingleText("Trbrna");
                        detail.cur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("CurrencyCode"));
                        detail.actNum = tmpBkReader.readedXElement.getSingleText("PayerAcNo");
                        detail.recAccNum = tmpBkReader.readedXElement.getSingleText("PayeeAcNo");
                        detail.recAccName = tmpBkReader.readedXElement.getSingleText("PayeeAcName");
                        detail.tranAmt = tmpBkReader.readedXElement.getSingleText("Amount");
                        detail.accBal = tmpBkReader.readedXElement.getSingleText("Balance");

                        String cdFlag = tmpBkReader.readedXElement.getSingleText("DCFlag");
                        String tranAmt = tmpBkReader.readedXElement.getSingleText("Amount");
                        if(tranAmt.contains("-")){
                            if("D".equals(cdFlag)){
                                detail.cdFlag="2";
                            }else if("C".equals(cdFlag)){
                                detail.cdFlag="1";
                            }
                            detail.tranAmt=tranAmt.replaceAll("-","");
                        }else {
                            if("D".equals(cdFlag)){
                                detail.cdFlag="1";
                            }else if("C".equals(cdFlag)){
                                detail.cdFlag="2";
                            }
                            detail.tranAmt=tranAmt;
                        }
                        //detail.askNumber = tmpBkReader.readedXElement.getSingleText("JnlNo");
                        detail.usage = tmpBkReader.readedXElement.getSingleText("Remark");
                        detail.abstr = pickupAbstr(tmpBkReader.readedXElement.getSingleText("Remark"), tmpBkReader.readedXElement.getSingleText("Smrytx"));
                        /*if (detail.tranAmt.startsWith("-")) {
                            detail.cdFlag = "C".equals(detail.cdFlag) ? "1" : "2";
                        } else {
                            detail.cdFlag = "C".equals(detail.cdFlag) ? "2" : "1";
                        }*/
                        detail.payCode = tmpBkReader.readedXElement.getSingleText("Smrytx");
                        retAts.writeRetDetailWithoutRespCode(detail);
                    }
                } else {
//                    tmpRespCode = "-1";
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg"));
                }
            } else {
//                tmpRespCode = "-1";
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpSbMsg.toString());
            }
        } else {
//            tmpRespCode = "-1";
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(tmpStrRet[0].toString());
        }

        return page;
    }
}
